package com.noah.project.dao

import com.noah.project.model.ReplyUser
import com.noah.project.model.User
import org.apache.ibatis.annotations.*
import org.springframework.stereotype.Repository

@Repository
interface UserDao {
    @Insert("INSERT INTO user(uid, name) VALUES(#{uid}, #{name})")
    @Options(useGeneratedKeys = true, keyProperty = "uid", keyColumn = "uid")//可以返回当前的uid,不加这行返回的是插入的行数
    fun insert(user: User): Long

    @Delete("DELETE FROM user WHERE uid =#{uid}")
    fun delete(id: Long?): Long

    @Update("UPDATE user SET name = #{name}, head = #{head}, about = #{about}  WHERE uid = #{uid}")
    fun update(user: User): Long

    @Update("UPDATE user SET token = #{token} WHERE uid = #{uid}")
    fun updateToken(user: User): Long

    @Select("SELECT * FROM user WHERE uid = #{uid}")
    fun findById(uid: Long?): User?//@Param("uid") 这个字段决定上面的#{uid}的赋值,uid: Long?的uid可以随便定义变量,如果没加@Param,uid: Long?的uid必须和#{uid}的一模一样才行

    @Select("SELECT * FROM user WHERE token = #{token}")
    fun findByToken(token: String?): User?//@Param("uid") 这个字段决定上面的#{uid}的赋值,uid: Long?的uid可以随便定义变量,如果没加@Param,uid: Long?的uid必须和#{uid}的一模一样才行

    @Select("SELECT * FROM user WHERE uid = #{uid}")
    fun findReplyById(uid: Long?): ReplyUser

    @Select("SELECT * FROM user")
    fun findAll(): List<User>
}
